package com.sip.stream.utils.zfy.muxer;

import android.media.MediaCodec;
import android.util.Log;

import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.zfy.GSMediaSampleData;
import com.sip.stream.utils.zfy.ZFYFilePath;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class ZFYPreRecordReader {
    private static final String TAG = "ZFYPreRecordReader";

    private FileInputStream bareStream;
    private String currentFilePath;
    private boolean firstKeyFrame;
    private String lastFilePath;
    private long lastTimeUS;
    private long nowTime;

    public ZFYPreRecordReader(ZFYPreRecordConsumer zFYPreRecordConsumer) {
        this.lastFilePath = null;
        this.currentFilePath = null;
        this.firstKeyFrame = false;
        this.lastFilePath = zFYPreRecordConsumer.getLastFilePath();
        this.currentFilePath = zFYPreRecordConsumer.getCurrentFilePath();
        String path = this.lastFilePath;
        if (path != null && !path.isEmpty()) {
            setUpInput(this.lastFilePath);
        } else {
            this.firstKeyFrame = true;
            setUpInput(this.currentFilePath);
        }
        this.lastTimeUS = zFYPreRecordConsumer.getLastTimeUS();
        this.nowTime = ZFYMediaManager.getInstance().getPresentationTimeUs();
        Log.i(TAG, "init " + this.nowTime + " lastTimeUS" + this.lastTimeUS);
    }

    private boolean setUpInput(String filePath) {
        Log.w(TAG, "setUpInput " + filePath);
        try {
            this.bareStream = new FileInputStream(filePath + ".braStream");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public GSMediaSampleData readNextSampleDataAction() {
        GSMediaSampleData readNextSampleData;
        if (!this.firstKeyFrame) {
            while (true) {
                readNextSampleData = readNextSampleData();
                if (readNextSampleData != null && readNextSampleData.getBufferInfo().flags == 1) {
                    long presentationTimeUs = readNextSampleData.getPresentationTimeUs();
                    Log.d(TAG, "presentationTimeUs: " + presentationTimeUs);
                    long j = this.nowTime;
                    if (j == 0 || j - presentationTimeUs <= this.lastTimeUS) {
                        break;
                    }
                }
            }
            this.firstKeyFrame = true;
            Log.d(TAG, "find first key frame");
            return readNextSampleData;
        }
        return readNextSampleData();
    }

    private GSMediaSampleData readNextSampleData() {
        FileInputStream fileInputStream = this.bareStream;
        if (fileInputStream == null) {
            Log.w(TAG, "bareStream == null");
            return null;
        }
        byte[] bArr = new byte[4];
        byte[] bArr2 = new byte[4];
        byte[] bArr3 = new byte[8];
        byte[] bArr4 = new byte[4];
        try {
            if (fileInputStream.read(bArr) < 4) {
                return tryNextVideoFile();
            }
            int bytes2Int = BarSteamTool.bytes2Int(bArr);
            if (this.bareStream.read(bArr2) < 4) {
                return tryNextVideoFile();
            }
            int bytes2Int2 = BarSteamTool.bytes2Int(bArr2);
            if (this.bareStream.read(bArr3) < 8) {
                return tryNextVideoFile();
            }
            long bytes2Long = BarSteamTool.bytes2Long(bArr3);
            if (this.bareStream.read(bArr4) < 4) {
                return tryNextVideoFile();
            }
            int bytes2Int3 = BarSteamTool.bytes2Int(bArr4);
            byte[] bArr5 = new byte[bytes2Int3];
            if (this.bareStream.read(bArr5) < bytes2Int3) {
                return tryNextVideoFile();
            }
            GSMediaSampleData gSMediaSampleData = new GSMediaSampleData();
            gSMediaSampleData.setAudio(bytes2Int == 0);
            ByteBuffer allocate = ByteBuffer.allocate(bytes2Int3);
            allocate.order(ByteOrder.LITTLE_ENDIAN);
            allocate.put(bArr5, 0, bytes2Int3);
            allocate.position(0);
            allocate.limit(bytes2Int3);
            gSMediaSampleData.setByteBuf(allocate);
            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
            bufferInfo.set(0, bytes2Int3, bytes2Long, bytes2Int2);
            gSMediaSampleData.setBufferInfo(bufferInfo);
            return gSMediaSampleData;
        } catch (IOException e) {
            e.printStackTrace();
            return tryNextVideoFile();
        }
    }

    private GSMediaSampleData tryNextVideoFile() {
        Log.w(TAG, "tryNextVideoFile now " + this.currentFilePath);
        try {
            this.bareStream.close();
            this.bareStream = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        String str = this.lastFilePath;
        if (str != null && !str.isEmpty()) {
            this.lastFilePath = null;
            if (setUpInput(this.currentFilePath)) {
                return readNextSampleData();
            }
        }
        cleanUpFiles();
        return null;
    }

    private void cleanUpFiles() {
        File[] listFiles = new File(ZFYFilePath.getPreRecordingCacheDirPath()).listFiles();
        if (listFiles != null) {
            for (File file : listFiles) {
                file.delete();
            }
        }
    }
}
